﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Grafica3
{
    public class PointLight
    {
        private Vector3 _lightPosition;
        public RenderTarget2D ShadowMap { get; private set; }
        public Vector3 Position
        {
            get
            { 
                return _lightPosition;
            }
            set
            {
                _lightPosition = value;
            }
        }
        public Color DiffuseColor { get; set; }
        public Color SpecularColor { get; set; }


        public PointLight(Vector3 Position, Color DiffuseColor, Color SpecularColor)
        {
            _lightPosition = Position;
            this.DiffuseColor = DiffuseColor;
            this.SpecularColor = SpecularColor;
        }

        public PointLight(Microsoft.Xna.Framework.Graphics.DirectionalLight Light)
        {
            _lightPosition = Vector3.Transform(Vector3.Zero, Matrix.CreateTranslation(-Light.Direction * 150));

            this.DiffuseColor = new Color(Light.DiffuseColor);
            this.SpecularColor = new Color(Light.SpecularColor);
        }

        public void LoadContent(GraphicsDevice graphic)
        {
            ShadowMap =
                new RenderTarget2D(
                    graphic,
                    graphic.Viewport.Width,
                    graphic.Viewport.Width,
                    false,
                    SurfaceFormat.Single,
                    DepthFormat.Depth24Stencil8);
        }

        public Matrix CreateLightViewProjectionMatrix(Camera camera)
        {
            var lightView = Matrix.CreateLookAt(Position, Vector3.Zero, Vector3.Up);
            var lightProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 1, 110, 170);
            return lightView * lightProjection;
        }
    }
}
